<?php

	/**
	 * Search controller where the index method will return a full html response for ajax.
	 * 
	 * @author bsuter
	 */
	Class SearchController Extends Framework
	{
		public $compression = true;

		/**
		 * @param $this->post->query [char] contains the search string form the input form
		 */
		public function index()
		{
			// show posts from text block but just for this language
			$obj = $this->db->select("cms_block", array(
				"cms_block.text", 
				"cms_block.title",
				"cms_nav.id" => "nav_id"
			), "LEFT JOIN (cms_nav) ON (cms_block.nav_id = cms_nav.id) WHERE text LIKE '%{$this->text_encode($this->post->query)}%' AND cms_block.language_id='{$this->i18n->id}' AND cms_nav.del=0 AND cms_nav.status=1", true);			
			
			$return = array();
			
			foreach($obj as $key => $val)
			{
				$return[] = array(
					'title' => $val->title,
					'text' => $val->text,
					'link' => Tools::BuildLinkById($val->nav_id),
					'id' => $val->nav_id
				);
			}
			
			// Tools::BuildLinkById();
				
			$this->view->assign(array(
				'search' => $return,
				'search_query' => $this->post->query,
				'search_num' => count($obj)
			));
			
			return $this->ajaxResponse($this->view->fetch('search.tpl'), false);
			
		}

	}